package com.larry.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * @ProjectName HDFS
 * @Author larry
 * @Date 12:21 2022/3/22
 * @Description TODO
 **/
public class HdfsClient {
    public static void main(String[] args) throws Exception {
        //获取hdfs客户端对象
        Configuration conf = new Configuration();
        //HADOOP_USER_NAME=atguigu
        //conf.set("fs.defaultFS","hdfs://hadoop102:9000");

        //FileSystem fs = FileSystem.get(conf);
        FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"),conf,"atguigu");

        fs.mkdirs(new Path("/test/dashen/haha"));

        //关闭资源
        fs.close();
    }

    private FileSystem fs;

    @Before
    public void init() throws URISyntaxException, IOException, InterruptedException {
        Configuration conf = new Configuration();
        fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), conf, "atguigu");
    }

    @After
    public void close() throws IOException {
        fs.close();
    }

    /**
     * 上传
     * @throws Exception
     */
    @Test
    public void testCopyFromLocalFile() throws Exception {
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), conf, "atguigu");

        fs.copyFromLocalFile(new Path("C:\\Users\\larry\\IdeaProjects\\HDFS\\src\\main\\resources\\haha.txt"),new Path("/test/dashen/haha.txt"));

        fs.close();
    }

    /**
     * 下载
     * @throws Exception
     */
    @Test
    public void testCopyToLocalFile() throws Exception {

        fs.copyToLocalFile(new Path("/test/dashen/haha.txt"),new Path("C:\\Users\\larry\\IdeaProjects\\HDFS\\src\\main\\resources\\haha2.txt"));
    }

    /**
     * 删除
     */
    @Test
    public void testDelete() throws IOException {
         fs.delete(new Path("/test/dashen/haha"),true);
    }

    /**
     * 更名
     * @throws IOException
     */
    @Test
    public void testRename() throws IOException {
         fs.rename(new Path("/test/dashen/haha.txt"),new Path("/test/dashen/haha3.txt"));
    }

    /**
     * 文件详情
     */
    @Test
    public void testListFiles() throws IOException {
        RemoteIterator<LocatedFileStatus> files = fs.listFiles(new Path("/"), true);

        while (files.hasNext()) {
            LocatedFileStatus fileStatus = files.next();

            System.out.println(fileStatus.getPath().getName());
            System.out.println(fileStatus.getPermission());
            System.out.println(fileStatus.getLen());


            BlockLocation[] blockLocations = fileStatus.getBlockLocations();

            for (BlockLocation blockLocation : blockLocations) {
                String[] hosts = blockLocation.getHosts();

                for (String host : hosts) {
                    System.out.println(host);
                }
            }

            System.out.println("-------------------------------------------");
        }
    }

    /**
     * 判断文件、文件夹
     */
    @Test
    public void testListStatus() throws IOException {
        RemoteIterator<LocatedFileStatus> files = fs.listFiles(new Path("/"), true);
        while (files.hasNext()) {
            LocatedFileStatus fileStatus = files.next();

            if (fileStatus.isDirectory()){
                System.out.println("文件夹:" + fileStatus.getPath().getName());
            }else if (fileStatus.isFile()){
                System.out.println("文件：" + fileStatus.getPath().getName());
            }
        }
    }


}
